var v = new Vue({
    el: '#app',
    data: function() {
        var isNumber = (rule, value, callback) => {
            var number = /^\d+$|^\d+[.]?\d+$/
            if (!number.test(value)) {
                callback(new Error("格式有误,只能为数字"));
            } else {
                callback();
            }
        }
        return {
            ruleForm: {
                basePay: 0,
                bonus: 0,
                yanglao: 0,
                yiliao: 0,
                shiye: 0,
                gongshang: 0,
                gongjijin: 0,
                zinv: [{
                    value: '0',
                    label: '无'
                }, {
                    value: '1',
                    label: '有1个',

                }, {
                    value: '2',
                    label: '有2个',

                }, {
                    value: '3',
                    label: '有3个',

                }],
                // fangdai: 0,
                fangdai: [{
                    value: '0',
                    label: '无'
                }, {
                    value: '1',
                    label: '房贷',

                }, {
                    value: '2',
                    label: '租房'
                }],

                laoren: [{
                    value: '0',
                    label: '无'
                }, {
                    value: '1',
                    label: '有',

                }],

            },
            rules: {
                basePay: [
                    { required: true, message: '不能为空哟！' },
                    { validator: isNumber, message: '只能输入数字，整数或小数' }
                ],
                bonus: [
                    { required: true, message: '不能为空哟！' },
                    { validator: isNumber, message: '只能输入数字，整数或小数' }
                ],
                yanglao: [
                    { required: true, message: '不能为空哟！' },
                    { validator: isNumber, message: '只能输入数字，整数或小数' }
                ],
                yiliao: [
                    { required: true, message: '不能为空哟！' },
                    { validator: isNumber, message: '只能输入数字，整数或小数' }
                ],
                shiye: [
                    { required: true, message: '不能为空哟！' },
                    { validator: isNumber, message: '只能输入数字，整数或小数' }
                ],
                gongjijin: [
                    { required: true, message: '不能为空哟！' },
                    { validator: isNumber, message: '只能输入数字，整数或小数' }
                ],
            },
            flag: true,
            value1: 0,
            value2: 0,
            value3: 0,
            resForm: [],
            fangdai: 0,
            laoren: 0,
            shuibiao: [

                [0.03, 0],
                [0.1, 210],
                [0.2, 1410],
                [0.25, 2660],
                [0.3, 4410],
                [0.35, 7160],
                [0.45, 15160]
            ],
            nian: [{
                all: 0,
                tax: 0,
                afterTax: 0,
            }],
            taxs: 0,
            shouru: {
                allShouru: 0,
                allTax: 0,
                gjj: 0,
                total: 0,
            }
        }
    },

    methods: {
        compu() {
            this.$refs.forms.validate(valid => {
                if (!valid) {
                    ELEMENT.Message.error("请输入正确格式！！")
                } else {
                    this.gongzi()
                }
            })

        },
        gongzi() {
            let _this = this
            this.flag = true
            this.resForm = []
            this.nian = [{
                all: 0,
                tax: 0,
                afterTax: 0
            }]
            this.shouru = {
                allShouru: 0,
                allTax: 0,
                gjj: 0,
            }
            if (parseInt(this.value1) === 1) {
                this.fangdai = 1000
            } else if (parseInt(this.value1) === 2) {
                this.fangdai = 1500
            } else {
                this.fangdai = 0
            }
            if (parseInt(this.value2) === 1) {
                this.laoren = 2000
            } else {
                this.laoren = 0
            }

            const gongjijins = this.ruleForm.gongjijin * 0.12
            const yanglaos = this.ruleForm.yanglao * 0.08
            const yiliaos = this.ruleForm.yiliao * 0.02
            const shiyes = this.ruleForm.shiye * 0.002
                // const shiyes = 0
            const zinvs = this.value3 * 1000
                //每月减去相关费用所剩
            const shuiqiangongzi = (this.ruleForm.basePay - gongjijins - yanglaos - yiliaos - shiyes - zinvs - this.fangdai - this.laoren)
            let taxs = 0
                //一年总收入，减去60000
            const text = shuiqiangongzi * 12 - 60000
            let shuju = {}
            if (text <= 0) {
                const shuihou1 = shuiqiangongzi + zinvs + this.fangdai + this.laoren
                for (let i = 0; i < 12; i++) {
                    shuju = {
                        months: (i + 1) + '月',
                        basePay: this.ruleForm.basePay,
                        yanglao: yanglaos,
                        yiliao: yiliaos,
                        shiye: shiyes,
                        gongjijin: gongjijins,
                        tax: 0,
                        allTax: 0,
                        afterTax: shuihou1,
                        allAfterTax: shuihou1 * (i + 1)
                    }
                    this.resForm.push(shuju)
                }
            } else {
                if (text > 0 && text <= 3000) {
                    taxs = (text * this.shuibiao[0][0] - this.shuibiao[0][1]).toFixed(2)
                } else if (text > 3000 && text <= 12000) {
                    taxs = (text * this.shuibiao[1][0] - this.shuibiao[1][1]).toFixed(2)
                } else if (text > 12000 && text <= 25000) {
                    taxs = (text * this.shuibiao[2][0] - this.shuibiao[2][1]).toFixed(2)
                } else if (text > 25000 && text <= 35000) {
                    taxs = (text * this.shuibiao[3][0] - this.shuibiao[3][1]).toFixed(2)
                } else if (text > 35000 && text <= 55000) {
                    taxs = (text * this.shuibiao[4][0] - this.shuibiao[4][1]).toFixed(2)
                } else if (text > 55000 && text <= 80000) {
                    taxs = (text * this.shuibiao[5][0] - this.shuibiao[5][1]).toFixed(2)
                } else if (text > 80000) {
                    taxs = (text * this.shuibiao[6][0] - this.shuibiao[6][1]).toFixed(2)
                }

                // const shuihou = shuiqiangongzi - taxs + zinvs + this.fangdai + this.laoren
                // let res = 0
                let shuihou = 0
                let totalTax = 0
                let totalCol = 0
                    // let totalGJJ = 0
                for (let i = 0; i < 12; i++) {
                    if ((shuiqiangongzi - 5000) * (i + 1) <= 36000) {
                        taxs = ((shuiqiangongzi - 5000) * this.shuibiao[0][0]).toFixed(2)
                            // res = shuiqiangongzi - taxs + zinvs + this.fangdai + this.laoren
                    } else if ((shuiqiangongzi - 5000) * (i + 1) <= 144000 && (shuiqiangongzi - 5000) * (i + 1) > 36000) {
                        if (i !== 0) {
                            if ((shuiqiangongzi - 5000) * i < 36000) {
                                taxs = ((36000 - (shuiqiangongzi - 5000) * i) * this.shuibiao[0][0] + ((shuiqiangongzi - 5000) * (i + 1) - 36000) * this.shuibiao[1][0]).toFixed(2)
                            } else {
                                taxs = ((shuiqiangongzi - 5000) * this.shuibiao[1][0]).toFixed(2)
                            }
                        } else {
                            taxs = ((shuiqiangongzi - 5000) * this.shuibiao[1][0]).toFixed(2)
                        }

                    } else if ((shuiqiangongzi - 5000) * (i + 1) <= 300000 && (shuiqiangongzi - 5000) * (i + 1) > 144000) {
                        if (i !== 0) {
                            if ((shuiqiangongzi - 5000) * i < 144000) {
                                taxs = ((144000 - (shuiqiangongzi - 5000) * i) * this.shuibiao[1][0] + ((shuiqiangongzi - 5000) * (i + 1) - 144000) * this.shuibiao[2][0]).toFixed(2)
                            } else {
                                taxs = ((shuiqiangongzi - 5000) * this.shuibiao[2][0]).toFixed(2)
                            }
                        } else {
                            taxs = ((shuiqiangongzi - 5000) * this.shuibiao[2][0]).toFixed(2)
                        }
                    } else if ((shuiqiangongzi - 5000) * (i + 1) <= 420000 && (shuiqiangongzi - 5000) * (i + 1) > 300000) {
                        console.log(1111111111)
                        if (i !== 0) {
                            if ((shuiqiangongzi - 5000) * i < 300000) {
                                taxs = ((300000 - (shuiqiangongzi - 5000) * i) * this.shuibiao[2][0] + ((shuiqiangongzi - 5000) * (i + 1) - 300000) * this.shuibiao[3][0]).toFixed(2)
                            } else {
                                taxs = ((shuiqiangongzi - 5000) * this.shuibiao[3][0]).toFixed(2)
                            }
                        } else {
                            taxs = ((shuiqiangongzi - 5000) * this.shuibiao[3][0]).toFixed(2)
                        }
                    } else if ((shuiqiangongzi - 5000) * (i + 1) <= 600000 && (shuiqiangongzi - 5000) * (i + 1) > 420000) {

                        if (i !== 0) {
                            if ((shuiqiangongzi - 5000) * i < 420000) {
                                taxs = ((420000 - (shuiqiangongzi - 5000) * i) * this.shuibiao[3][0] + ((shuiqiangongzi - 5000) * (i + 1) - 420000) * this.shuibiao[4][0]).toFixed(2)
                            } else {
                                taxs = ((shuiqiangongzi - 5000) * this.shuibiao[4][0]).toFixed(2)
                            }
                        } else {
                            taxs = ((shuiqiangongzi - 5000) * this.shuibiao[4][0]).toFixed(2)
                        }
                    } else if ((shuiqiangongzi - 5000) * (i + 1) <= 960000 && (shuiqiangongzi - 5000) * (i + 1) > 600000) {
                        if (i !== 0) {
                            if ((shuiqiangongzi - 5000) * i < 600000) {
                                taxs = ((600000 - (shuiqiangongzi - 5000) * i) * this.shuibiao[4][0] + ((shuiqiangongzi - 5000) * (i + 1) - 600000) * this.shuibiao[5][0]).toFixed(2)
                            } else {
                                taxs = ((shuiqiangongzi - 5000) * this.shuibiao[5][0]).toFixed(2)
                            }
                        } else {
                            taxs = ((shuiqiangongzi - 5000) * this.shuibiao[5][0]).toFixed(2)
                        }
                    } else if ((shuiqiangongzi - 5000) * (i + 1) > 960000) {
                        if (i !== 0) {
                            if ((shuiqiangongzi - 5000) * i < 960000) {
                                taxs = ((960000 - (shuiqiangongzi - 5000) * i) * this.shuibiao[5][0] + ((shuiqiangongzi - 5000) * (i + 1) - 960000) * this.shuibiao[6][0]).toFixed(2)
                            } else {
                                taxs = ((shuiqiangongzi - 5000) * this.shuibiao[6][0]).toFixed(2)
                            }
                        } else {
                            taxs = ((shuiqiangongzi - 5000) * this.shuibiao[6][0]).toFixed(2)
                        }
                    }
                    totalTax += Number(taxs)
                    const kd = totalTax.toFixed(2)
                    shuihou = shuiqiangongzi - taxs + zinvs + this.fangdai + this.laoren
                    totalCol += shuihou
                        // totalGJJ += gongjijins
                        // (shuiqiangongzi - 5000) * 0.03
                    shuju = {
                        months: (i + 1) + '月',
                        basePay: this.ruleForm.basePay,
                        yanglao: yanglaos,
                        yiliao: yiliaos,
                        shiye: shiyes,
                        gongjijin: gongjijins,
                        tax: taxs,
                        allTax: kd,
                        afterTax: shuihou,
                        allAfterTax: totalCol.toFixed(2)
                            // allAfterTax: totalCol
                    }
                    console.log(gongjijins)
                    console.log('**********')
                    console.log(_this.shouru.gjj)
                    this.shouru.gjj += (gongjijins * 2)
                    this.resForm.push(shuju)
                }
            }
            this.nianzhongjiang()
        },

        //年终奖个税计算
        nianzhongjiang() {
            this.nian[0].all = this.ruleForm.bonus
            const bos = this.ruleForm.bonus
            const text = bos / 12

            // if (text)
            // let taxs = 0
            if (Number(this.ruleForm.bonus) === 0) {

                this.taxs = 0
            } else {

                if (text > 0 && text <= 3000) {
                    this.taxs = bos * this.shuibiao[0][0] - this.shuibiao[0][1]
                } else if (text > 3000 && text <= 12000) {
                    this.taxs = bos * this.shuibiao[1][0] - this.shuibiao[1][1]
                } else if (text > 12000 && text <= 25000) {
                    this.taxs = bos * this.shuibiao[2][0] - this.shuibiao[2][1]
                } else if (text > 25000 && text <= 35000) {
                    this.taxs = bos * this.shuibiao[3][0] - this.shuibiao[3][1]
                } else if (text > 35000 && text <= 55000) {
                    this.taxs = bos * this.shuibiao[4][0] - this.shuibiao[4][1]
                } else if (text > 55000 && text <= 80000) {
                    this.taxs = bos * this.shuibiao[5][0] - this.shuibiao[5][1]
                } else if (text > 80000) {
                    this.taxs = bos * this.shuibiao[6][0] - this.shuibiao[6][1]
                }
            }
            this.nian[0].tax = this.taxs
            this.nian[0].afterTax = this.nian[0].all - this.nian[0].tax

            this.shourus()
        },
        //所有收入及所有个税
        shourus() {
            this.shouru.allShouru = Number(this.nian[0].afterTax) + Number(this.resForm[11].allAfterTax)
            this.shouru.allTax = Number(this.nian[0].tax) + Number(this.resForm[11].allTax)
            this.shouru.total = this.shouru.allShouru + this.shouru.gjj
        },
        change() {
            if (!this.flag) return
            this.ruleForm.gongjijin = this.ruleForm.basePay
            this.ruleForm.yanglao = this.ruleForm.basePay
            this.ruleForm.shiye = this.ruleForm.basePay
            this.ruleForm.yiliao = this.ruleForm.basePay
        },
    }
})